﻿using System.IO;
using System.Linq;
using JustAgile.Html.Linq;
using NovelDB.Entity;
using System.Collections.Generic;

namespace NovelLibEx.Parser
{
    public class AiduzParser : INovelParser
    {
        /// <summary>
        /// 将小说目录结构解析出来
        /// 所有的目录和链接都是包含在tr中的。所以可以遍历tr就能够一一找出所有内容
        /// </summary>
        /// <param name="baseUrl">小说中目录的前缀</param>
        /// <param name="strContent">目录内容</param>
        /// <returns>解析之后的小说结构体</returns>
        public Novel ExtractNovelCatelogLinks(string baseUrl, string strContent)
        {
            HDocument doc = HDocument.Parse(strContent);
            // 首先，找出是小说链接的内容。这样可以过滤掉广告信息
            var trs = (from tr in doc.Descendants("tr")
                            select tr
                            ).ToList();

            Novel novelOfWeb = new Novel { NovelChapters = new List<NovelChapter>()};
            NovelChapter chap = null;
            int idOfChapter = 0;
            int idOfLink = 0;

            foreach (var tr in trs)
            {
                var dvcssDivs = (from dvcss in tr.Descendants("div")
                                 where dvcss.FirstAttribute.Name == "class"
                                 && dvcss.Attribute("class").Value == "dvcss"
                                 select dvcss).FirstOrDefault();

                var dccssDivs = (from dccss in tr.Descendants("div")
                                 where dccss.FirstAttribute.Name == "class"
                                 && dccss.Attribute("class").Value == "dccss"
                                 select dccss).ToList();

                if (dvcssDivs != null)
                {
                    var chapName = dvcssDivs.Value.Trim();
                    chap = new NovelChapter { Name = chapName, ChapterId = idOfChapter, NovelLinks = new List<NovelLink>() };
                    
                    idOfChapter++;

                    novelOfWeb.NovelChapters.Add(chap);
                }
                else if (dccssDivs.Count > 1)
                {
                    foreach (var dcc in dccssDivs)
                    {
                        var link = (from l in dcc.Descendants("a")
                                    select l).FirstOrDefault();

                        if (link != null)
                        {
                            NovelLink novelLink = new NovelLink
                            {
                                Href = Path.GetFileName(link.Attribute("href").Value),
                                Title = link.Attribute("alt").Value,
                                Description = link.Value,
                                LinkId = idOfLink,
                                SiteUrl = baseUrl,
                                IsUpdated = false,
                                UpdateTime = ""
                            };

                            chap.NovelLinks.Add(novelLink);
                            idOfLink++;
                        }
                    }
                }
            }

            return novelOfWeb;
        }

        /// <summary>
        /// 将小说正文中的广告信息摘录出来
        /// </summary>
        /// <param name="strContent">小说正文</param>
        /// <returns>去广告之后的内容</returns>
        public string RemoveAd(string strContent)
        {
            HDocument hDoc = HDocument.Parse(strContent);
            var nvContent = (from content in hDoc.Descendants("div")
                             where content.FirstAttribute.Name == "id"
                             && content.Attribute("id").Value == "content"
                                 select content).First();

            string strResult = nvContent.Value.Replace("&nbsp;&nbsp;&nbsp;&nbsp;", "");

            return strResult;
        }
    }
}
